<?php

namespace App\Http\Controllers\manage;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\managerModel;
use Illuminate\Support\Facades\DB;

class wordController extends Controller
{
    function index()
    {
        $title = "管理分词";
        return view("manage\word", compact('title'));
    }

    function getWords(Request $request)
    {
        $username = $request->session()->get("user");
        $MID = managerModel::where("user", $username)->first()->MID;
        $words = DB::table("lables")
            ->where([
                ["creatorType", 1]],
                ["creator", $MID])->get();
        $return['words'] = array();
        $return['total'] = 0;
        foreach ($words as $word) {
            array_push($return['words'], $word);
            $return['total']++;
        }

        return json_encode($return);
    }

    function addWords(Request $request)
    {
        $username = $request->session()->get("user");
        $MID = managerModel::where("user", $username)->first()->MID;
        $text = $request->input(("text"));

        $count = DB::table("lables")->where([
            ["lable", $text]
        ])->count();
        if ($count) {
            return "exist";
        }

        $res = DB::table("lables")->insert([
            "lable" => $text,
            "creator" => $MID,
            "creatorType" => 1,
            "tasty" => 0
        ]);
        if ($res) {
            return "OK";
        }
        return "";
    }

    function deleteWords(Request $request)
    {
        $LID = $request->input("LID");

        $res = DB::table('lables')->where("LID", $LID)->delete();

        return "OK";
    }
}
